import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.lang.runtime.ObjectMethods;

class Student{
    public int studentId;
    public int classId;

}

@WebServlet("/PostParameter2")
public class PostParameter2Servlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理body为jason格式的数据
        //直接把req对象里的body完整读取出来 getInputStream
        //在流对象中读取多少个字节 取决于Content-Length
//        int length = req.getContentLength();
//        byte[] buffer = new byte[length];
//
//        //传数据填充Buffer
//        //通过contentlength得到body的长度,再按照这个长度从请求对象中读取数据,也就把直线在postman里写好的数据读取到了
//        InputStream inputStream = req.getInputStream();
//        inputStream.read(buffer);
//
//        //打印出这个字节数组构造成的String
//        String body = new String(buffer, 0, length, "utf8");
//        System.out.println("body = "+body);
//        resp.getWriter().write(body);

        //使用jackson涉及到核心对象
        ObjectMapper objectMapper = new ObjectMapper();
        //readVlude作用是吧json格式的数据传转化为 Java对象
        Student student = objectMapper.readValue(req.getInputStream(), Student.class);
//        resp.getWriter().write(body);
        System.out.println(student.studentId+", "+student.classId);
    }
}
